package com.dminvoicemanager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.dminvoicemanager.tabs.DM_Retailer_Info_Main_Tabs;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class DM_NoneProdCall_Reason extends Activity {
	private String DBNAME = "dminvoicemanager.db";
	private String TOWN_DETAILS = "fTown";
	private String ITEM_DEBTOR = "fDebtor";
	private String RETAILER_SUM = "FRetailScrSum";
	private String DAILY_NONE_PRDCALLS_HEAD = "fNonePrd";
	TextView txtHeading;
	SQLiteDatabase mydb;
	String retailerCode,TagId,txtDate;
	
	EditText etdmstReason;
	Button btnUpdate,btnBackAll;
	Spinner spinner ;

	public void btnBack(View view) {
		Intent myIntent = new Intent(view.getContext(), 
				DM_ProductiveCalls.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		myIntent.putExtra("TAGID", "3");
		startActivityForResult(myIntent, 0);
		finish();
	}
	
	public void btnBackAll(View view) {
		Intent myIntent = new Intent(view.getContext(), 
				DM_ProductiveCalls.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		myIntent.putExtra("TAGID", "3");
		startActivityForResult(myIntent, 0);
		finish();
	}


	public void DisplayTownDetails(View view) {
		Button townDet = (Button) view;
		String townCode = townDet.getText().toString();
		Toast.makeText(getApplicationContext(), "townCode :" + townCode,
				Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_noneprodcall_reason);
		Bundle bundle = getIntent().getExtras();
		retailerCode = bundle.getString("retailerCode1");
		Log.v("", "retailerCode PCallsReason/ retailerCode -::" + retailerCode + ":"+ retailerCode);
	//	etdmstReason = (EditText) findViewById(R.id.dmretcode);
		spinner = (Spinner) findViewById(R.id.spinner1);
		btnUpdate = (Button) findViewById(R.id.btninvupdateitem);
		btnBackAll = (Button) findViewById(R.id.btninvheader);
		//txtHeading.setText("Retailer Master");
		DisplayRetailerList();
		
		btnUpdate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				
				UpdateInvoiceItem();

			}

		});
		
		btnBackAll.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				
				Intent myIntent = new Intent(view.getContext(), 
						DM_ProductiveCalls.class);
				myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				myIntent.putExtra("TAGID", "3");
				startActivityForResult(myIntent, 0);
				finish();

			}

		});

	}
	
	
	
	
	// Update current invoice item details in to DQLite DB
		protected void UpdateInvoiceItem() {
			// TODO Auto-generated method stub
			String towncode =  retailerCode;//"A53440102";//="A53440094";
			Log.v("", "TagID / TagID -::" + towncode + ":"+ towncode);
			txtDate = getCurrentTimeString().toString(); 

			Log.v("", "txtDate / txtDate -::" + txtDate + ":"+ txtDate);
		//	etdmstReason.setEnabled(false);
	
			mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
			
			Cursor getCurrentRecDet = null;
			try {
                
				getCurrentRecDet = mydb.query(DAILY_NONE_PRDCALLS_HEAD, null, "debname ='"
						+ towncode  + "' AND txndate='" + txtDate + "'", null, null, null, null);

				Log.v("", "TagID / TagID -::" + getCurrentRecDet.getCount() + ":"+ getCurrentRecDet.getCount());
			} catch (Exception e) {
			}
			if (getCurrentRecDet != null) {
				if (getCurrentRecDet.getCount() > 0) {
					while (getCurrentRecDet.moveToNext()) {
						ContentValues newitemdetailrecord = new ContentValues();
						String Txndate = getCurrentRecDet.getString(2);
						String DebCode = getCurrentRecDet.getString(3);
						String DebName = getCurrentRecDet.getString(4);
						String Status = "NP";
						//String Reason=  etdmstReason.getText().toString();
						String ReasonOld=  String.valueOf(spinner.getSelectedItem()) ;
						String[] parts = ReasonOld.split("-");
						String ReasonOld1 = parts[0]; // 004
						String Reason= parts[1]; // 034556
						
						
						String stldmhedSync = getCurrentRecDet.getString(6);
						
						
						Log.v("", "Debcode / Debcode -::" + DebCode + ":"+ DebCode);
						Log.v("", "DebName / DebName -::" + DebName + ":"+ DebName);
						Log.v("", "Status / Status -::" + Status + ":"+ Status);
						Log.v("", "Txndate / Txndate -::" + Txndate + ":"+ Txndate);
						Log.v("", "Reason / Reason -::" + Reason + ":"+ Reason);
						Log.v("", "stldmhedSync / stldmhedSync -::" + stldmhedSync + ":"+ stldmhedSync);
						
						newitemdetailrecord.put("Reason", Reason.toString());
						newitemdetailrecord.put("Status", Status.toString());
						newitemdetailrecord.put("stldmhedSync", "0");
						
						mydb.update(DAILY_NONE_PRDCALLS_HEAD, newitemdetailrecord, "debname ='"
								+ towncode + "' AND txndate='" + txtDate + "'", null);
						
						final AlertDialog alertDialog = new AlertDialog.Builder(
								DM_NoneProdCall_Reason.this).create();
						// Setting Dialog Title
						alertDialog.setTitle("UPDATE");
						// Setting Dialog Message
						alertDialog.setMessage("Record Updated Successfully");
						// Setting Icon to Dialog
						alertDialog.setIcon(R.drawable.warning);
						// Setting OK Button
						alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								alertDialog.dismiss();
								
							}
						});
						// Showing Alert Message
						alertDialog.show();
						alertDialog.setCanceledOnTouchOutside(false);
					}
				}
			}
		
		}

	private void DisplayRetailerList() {
		    
		 	String towncode =retailerCode;//"A53440102";
			mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
			Cursor selectAllMeterRead = null;
			
			txtDate = getCurrentTimeString().toString();
			
			String myString = "some value"; //the value you want the position for

			
			//set the default according to value
			//spinner.setSelection(spinnerPosition);

			try {
				selectAllMeterRead = mydb.query(DAILY_NONE_PRDCALLS_HEAD, null, "debname='" + towncode + "' AND txndate='" + txtDate + "'", null,
						null, null, null);
				Log.v("", "txtDate / txtDate -:" + txtDate + ":"+ txtDate);
				Log.v("", "Count / Update -:" + selectAllMeterRead.getCount() + ":"+ selectAllMeterRead.getCount());
			} catch (Exception e) {
			}
			if (selectAllMeterRead != null) {
				if (selectAllMeterRead.getCount() > 0) {
					while (selectAllMeterRead.moveToNext()) {
						
						String PreRd = selectAllMeterRead.getString(7);
						if(PreRd.isEmpty())
						{
						spinner.setEnabled(true);
						}else if(PreRd=="Shop closed"){	
						spinner.setSelection(0);
						spinner.setContentDescription(PreRd);
						Log.v("", "PreRd / PreRd -:" + PreRd + ":"+ PreRd);
						btnUpdate.setEnabled(true);
						}else if(PreRd.equalsIgnoreCase("no Cash")){
							spinner.setSelection(2);
						}
						else if(PreRd.toString().equalsIgnoreCase("return cheque")){
							spinner.setSelection(4);
							Log.v("", "PreRd5 / PreRd5 -:" + PreRd + ":"+ PreRd);
						}
						else if(PreRd.toString().equalsIgnoreCase("dealer out")){
							spinner.setSelection(1);
							Log.v("", "PreRd5 / PreRd5 -:" + PreRd + ":"+ PreRd);
						}
						else if(PreRd.toString().equalsIgnoreCase("pending payment")){
							spinner.setSelection(3);
							Log.v("", "PreRd5 / PreRd5 -:" + PreRd + ":"+ PreRd);
						}
			}
				}
			}
			selectAllMeterRead.close();
			mydb.close();

	}
	
	//Validating Empty Reason
	private void ValidateEmptyResn(){
		
		
	}
	
	// get current data and time
	private String getCurrentTimeStringCustom() {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String formattedDate = df.format(c.getTime());
		return formattedDate;
	}

	private String getCurrentTimeString() {
		Date dateNow = new Date();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat ddMMMyyFormat = new SimpleDateFormat("dd/MM/yyyy");
		String date_to_string = ddMMMyyFormat.format(dateNow);
		return date_to_string;
	}



	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}

}
